package edu.kpi.pzks2.simulator.results;

import edu.kpi.pzks2.simulator.links.SimplexChannel;
import edu.kpi.pzks2.simulator.processing.Processor;

public class ChannelHistoryBean extends HistoryBean {
    private String str;
    private final boolean bothBusy;
    
    public ChannelHistoryBean(
            final SimplexChannel msgAtoB,
            final SimplexChannel msgBtoA,
            final Processor procA,
            final Processor procB) {
        str = "";
        if (msgAtoB.isTransmittingNow()) {
            if (msgAtoB.getLinkedProc() != null && msgAtoB.getMsg() != null && msgAtoB.isBusy()) {
                str +=
//                  String.format(
//                          "%dto%d: %dfor%d",
//                          procA.getProcId(),
//                          procB.getProcId(),
//                          msgAtoB.getMsg().getResultOfTask(),
//                          msgAtoB.getMsg().getTargetTask());
                        String.format(
                                "%d-%d (%d)",
                                msgAtoB.getMsg().getResultOfTask(),
                                msgAtoB.getMsg().getTargetTask(),
                                procB.getProcId());
                
                if (msgBtoA.isTransmittingNow()) {
                    str += " | ";
                }
            }
        }
        
        if (msgBtoA.isTransmittingNow()) {
//            str += String.format(
//                    "%dto%d: %dfor%d",
//                    procB.getProcId(),
//                    procA.getProcId(),
//                    msgBtoA.getMsg().getResultOfTask(),
//                    msgBtoA.getMsg().getTargetTask());
            str += String.format(
                    "%d-%d (%d)",
                    msgBtoA.getMsg().getResultOfTask(),
                    msgBtoA.getMsg().getTargetTask(),
                    procA.getProcId());
        }
        
        bothBusy = msgAtoB.isBusy() && msgBtoA.isBusy();
    }
    
    public boolean isBothBusy() {
        return bothBusy;
    }
    
	@Override
	public String toString() {
	    return str;
	}
}
